package com.intel.factorytest.bandtx;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.content.Intent;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import android.content.res.Configuration;
import android.widget.Button;
import android.util.Log;
import com.intel.factorytest.R;

import android.os.SystemProperties;
import android.os.SystemClock;
import com.intel.factorytest.util.Util;
import com.intel.factorytest.util.LogUtils;

public class BandTest extends Activity implements OnItemClickListener{

    private static final String TAG = "BandTest";
    ListView listView;
    private static final String[] BANDNAME = 
            new String[] {"LTE TX Band 1", "LTE TX Band 2", "LTE TX Band 3", "LTE TX Band 4", "LTE TX Band 5", "LTE TX Band 7", "LTE TX Band 8",
                          "LTE TX Band 12", "LTE TX Band 13", "LTE TX Band 19", "LTE TX Band 20", "LTE TX Band 21", "LTE TX Band 28",
                          "LTE TX Band 48", "3G TX Band 1", "3G TX Band 2", "3G TX Band 5", "3G TX Band 8"};
   
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bandtest_main);
        listView = (ListView)findViewById(R.id.band_list_view);
        listView.setOnItemClickListener(this);
        List<String> list = new ArrayList<String>();
        for(String bandid : BANDNAME) {
            LogUtils.logi( "bandid" + bandid);
            list.add(bandid);
        }
        
        ItemAdapter adapter = new ItemAdapter(getApplicationContext(),list);
        listView.setAdapter(adapter);        
    }


    @Override
    protected void onResume() {
        LogUtils.logi( "on onResume");
        super.onResume();
        SystemProperties.set("sys.cit.key.test","true");
        //if(!Util.isFtmMode) {
        enterFtmMode();
        //}
    }
    
    @Override
    protected void onPause() {
        LogUtils.logi( "onPause");
        SystemProperties.set("sys.cit.key.test","false");        
        super.onPause();
    }


    @Override
    protected void onStop() {
        LogUtils.logi( "onStop");
        //if(Util.isFtmMode) {
           //exitFtmMode();
        //}
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        LogUtils.logi( "onDestroy");         
        super.onDestroy();        
        sendDiagCmd("null");
    }

    private class ItemAdapter extends BaseAdapter{

        List<String> mList;
        Context mContext;
        public ItemAdapter(Context context,List<String> list){
            mList = list;
            mContext = context;
        }


        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mList == null ? 0 : mList.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return mList == null ? null :mList.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub                
            LogUtils.logi( "mContext" + mContext);
            LogUtils.logi( "arg1" + arg1 + ",arg0 = " + arg0);
            if(arg1 == null){
                arg1 = LayoutInflater.from(mContext).inflate(R.layout.band_item, null);
                TextView itemName = (TextView)arg1.findViewById(R.id.bandname);                
                LogUtils.logi( "setText" + mList.get(arg0));
                itemName.setText(mList.get(arg0));
            }
            return arg1;
        }

    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        LogUtils.logi( "arg2 = " + arg2);
        sendDiagCmd("null");
        Intent bandtestingIntent = new Intent();
        bandtestingIntent.setClassName("com.intel.factorytest",
                    "com.intel.factorytest.bandtx.BandTestingActivity");
        bandtestingIntent.putExtra("bandname", BANDNAME[arg2]);
        startActivity(bandtestingIntent);
    }
    
     private void enterFtmMode() {
         sendDiagCmd("ftmin");
         Util.isFtmMode = true;
     }
     
     private void exitFtmMode() {
         sendDiagCmd("ftmout");         
         Util.isFtmMode = false;
     }

     private void sendDiagCmd(String band) {
         SystemProperties.set("persist.sys.chamber.bandtest",band);
     }

     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 
     }

}
